package org.bravo369.inflightxt;


import org.bravo369.inflightxt.services.GpsLocationListener;
import org.bravo369.inflightxt.services.TiltPositionListener;
import org.bravo369.inflightxt.utils.Globals;
import org.bravo369.inflightxt.utils.Exitable;
import org.bravo369.inflightxt.utils.Globals.SyncMode;
import org.bravo369.inflightxt.utils.RestClient;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DataCollectorView extends Activity implements Exitable {

  private enum CollectorState {
    STOPPED,
    RUNNING
  };
  private CollectorState state;
  private GpsLocationListener gpsListener;
  private TiltPositionListener tiltListener;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data_view);

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.data_view, null);

    state = CollectorState.STOPPED;
    gpsListener = new GpsLocationListener(this);
    tiltListener = new TiltPositionListener(this);

    Globals.getInstance().addExitHandler(this);

    Button buttonRecord = (Button) findViewById(R.id.buttonRecord);
    buttonRecord.setOnClickListener(new OnClickListener() {
      public void onClick(View v) { toggleCollectorState(); }
    });

    Button buttonSession = (Button) findViewById(R.id.buttonSession);
    buttonSession.setOnClickListener(new OnClickListener() {
      public void onClick(View v) { getNewSession(); }
    });

    Button buttonSyncMode = (Button) findViewById(R.id.buttonSyncMode);
    buttonSyncMode.setOnClickListener(new OnClickListener() {
      public void onClick(View v) { toggleSyncMode(); }
    });
  }

  @Override
  public void onExit() {
    gpsListener.exit();
    tiltListener.exit();
    finish();
  }

  private void toggleSyncMode() {
    Globals globals = Globals.getInstance();
    Button buttonSyncMode = (Button) findViewById(R.id.buttonSyncMode);
    switch (globals.mode) {
    case SYNC_TO_SERVER:
      globals.mode = Globals.SyncMode.NO_SYNC_TO_SERVER;
      buttonSyncMode.setText(R.string.txtNoSyncMode);
      break;
    case NO_SYNC_TO_SERVER:
      globals.mode = Globals.SyncMode.SYNC_TO_SERVER;
      buttonSyncMode.setText(R.string.txtSyncMode);
      break;
    }
  }

  private void getNewSession() {
    Globals globals = Globals.getInstance();
    TextView sessionView = (TextView) findViewById(R.id.txtSession);
    RestClient client = new RestClient(globals.apiUri);
    client.AddParam("method", "session");
    client.AddHeader("Authorization", globals.authKey);
    try {
      client.Execute(RestClient.RequestMethod.GET);
      JSONObject result = (JSONObject) new JSONTokener(client.getResponse()).nextValue();
      String sessionId = result.getJSONObject("Session").getString("id_key");
      sessionView.setText(sessionId);
      globals.currentSession = sessionId;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  private void toggleCollectorState() {
    Button buttonRecord = (Button)findViewById(R.id.buttonRecord);
    TextView sessionView = (TextView) findViewById(R.id.txtSession);
    Globals globals = Globals.getInstance();
    if (sessionView.getText().equals("0") && globals.mode == SyncMode.SYNC_TO_SERVER)
      return;
    switch (state) {
    case STOPPED:
      buttonRecord.setText("Stop");
      gpsListener.registerListener();
      tiltListener.registerListener();
      state = CollectorState.RUNNING;
      break;
    case RUNNING:
      buttonRecord.setText("Record");
      gpsListener.unregisterListener();
      tiltListener.unregisterListener();
      state = CollectorState.STOPPED;
      break;
    }
  }
}
